var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var jiami = require('../../../module/jiami');

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('admin/user/rolelist')
});

router.get('/api/rolelist',async (req,res)=>{
    let page = parseInt(req.query.page);  //第几页
    let limitNum = parseInt(req.query.limit); //限制多少条
    let sqlStr = "select * from role limit ?,?";
    let arr = [(page-1)*limitNum,limitNum]
    let result = await sqlQuery(sqlStr,arr);
     //获取总数
    let sqlStr1 = "select count(id) as number from role"
    let result1 = await sqlQuery(sqlStr1)
    let count = result1[0].number
    let options ={
        "code": 0,
        "msg": "",
        "count": count,
        "data": Array.from(result)
      }
      res.json(options)
})


//添加角色
router.get('/addrole',(req,res)=>{
    res.render('admin/user/addrole')
})


//获取前端提交的数据
router.post('/addrole',async (req,res)=>{
    // console.log("获取的数据：",req.body);
    //1. 修改角色表
    //2. 修改角色与权限的关系表

    //获取角色名称
    let rolename = req.body.rolename
    let brief = req.body.brief
    //内容插入
    let sqlStr = "insert into role (rolename,brief) values (?,?)";
    await sqlQuery(sqlStr,[rolename,brief])

    //2. 修改角色与权限的关系表
    //获取权限数组
    let authlist = req.body.authlist
    //循环
    authlist.forEach(async (item,i) => {
        let id = item.value;
        let sqlStr2 = " insert into role_auth (roleid,authid) values ((select id from role where rolename = ?),?)"
        await sqlQuery(sqlStr2,[rolename,id])

    });
    res.json({
        'state':'ok',
        'content':'数据插入成功'
    })
})



//删除角色
router.get('/delrole',async(req,res)=>{
    let userid = req.query.userid
    let sqlStr = "delete from role where id = ?";
    let result = await sqlQuery(sqlStr,[userid])
    res.json({
      state:"ok",
      content:Array.from(result)
    })
  })




  //编辑角色
  router.get('/editrole',async function (req, res, next) {
    //获取用户id
    let userid = req.query.id;
    let sqlStr = "select * from role where id = ?";
    let result = await sqlQuery(sqlStr,[userid])
    let role = result[0]
    let options = {role};
    res.render('admin/user/roleedit',options);

  });


  //修改用户信息，post提交
  router.post('/editrole',async(req,res)=>{
    let rolename = req.body.rolename
    let brief = req.body.brief
    let roleid = req.body.roleid

    //更新数据
    let sqlStr = "update role set rolename=?, brief=? where id=? ";
    let arr = [rolename,brief,roleid]
    await sqlQuery(sqlStr,arr);
        res.json({
          state:"ok",
          content:"角色信息成功"
        })
  })


//验证添加角色时角色名称是否已存在
router.post('/testing',async(req,res)=>{
  //判断用户名是否存在
  let rolename = req.body.rolename
  let sqlStr1 = "select * from role where rolename =?"
  let result1 = await sqlQuery(sqlStr1,[rolename])
  if(result1.length == 0){
      res.json({
          state:"ok",
          content:"没有匹配到可以添加"
      })
  }else{
      res.json({
          state:"fail",
          content:"角色名称已存在"
      })
  }
})





module.exports = router;